package org.yu.register.item;

import com.google.common.collect.ImmutableList;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.registries.RegistryObject;
import org.yu.register.SoundEventRegistry;
import org.yu.register.entity.JudgementOfShamashEntity;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Random;

@ParametersAreNonnullByDefault
public class JudgementOfShamash extends SwordItem {

    public JudgementOfShamash(Properties pProperties) {
        super(Tiers.DIAMOND, 20, -3.2f, pProperties);
    }

    public static final ImmutableList<RegistryObject<SoundEvent>> EX_SOUNDS = ImmutableList.of(
            SoundEventRegistry.dlkLy,
            SoundEventRegistry.dlkQs,
            SoundEventRegistry.dlkPx
    );
    public static final ImmutableList<RegistryObject<SoundEvent>> ATTACK_SOUNDS = ImmutableList.of(
            SoundEventRegistry.dlkA1,
            SoundEventRegistry.dlkA2,
            SoundEventRegistry.dlkA3
    );
    public static final Random RANDOM = new Random();

    public void playAttackSounds(Entity entity) {
        entity.level.playSound(null, entity, ATTACK_SOUNDS.get(RANDOM.nextInt(ATTACK_SOUNDS.size())).get(), SoundSource.PLAYERS, 3, 1);
    }
    @Nonnull
    @Override
    public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) {
        var item = pPlayer.getItemInHand(pUsedHand);
        pPlayer.startUsingItem(pUsedHand);
        pPlayer.getCooldowns().addCooldown(this, getUseDuration(item) + 20);
        pLevel.playSound(pPlayer, pPlayer, EX_SOUNDS.get(RANDOM.nextInt(EX_SOUNDS.size())).get(), SoundSource.PLAYERS, (float) (3f+Math.random()*0.5f), 1);
        return InteractionResultHolder.consume(item);
    }

    @Nonnull
    @Override
    public ItemStack finishUsingItem(ItemStack pStack, Level pLevel, LivingEntity pLivingEntity) {
        if (!pLevel.isClientSide) {
            JudgementOfShamashEntity judgement = new JudgementOfShamashEntity(pLivingEntity, pLevel);
            judgement.shootFromRotation(pLivingEntity, pLivingEntity.getXRot(), pLivingEntity.getYRot(), 0.0F, 1.5F, 0.1F);
            pLevel.addFreshEntity(judgement);
            pStack.hurtAndBreak(10,pLivingEntity,(k)->{});
        }
        return super.finishUsingItem(pStack, pLevel, pLivingEntity);
    }

    @Override
    public void onUseTick(Level pLevel, LivingEntity pLivingEntity, ItemStack pStack, int pRemainingUseDuration) {
        if (pLevel instanceof ServerLevel serverLevel) {
            if (pRemainingUseDuration > 0) {
                var pos = pLivingEntity.getEyePosition();
                pLivingEntity.invulnerableTime=15;
                serverLevel.sendParticles(ParticleTypes.FLAME, pos.x, pos.y + 2 + Math.max(0, pRemainingUseDuration - 20), pos.z, 2, 0, 0.5, 0, .1);
            } else {
                finishUsingItem(pStack, pLevel, pLivingEntity);
            }
        }
        super.onUseTick(pLevel, pLivingEntity, pStack, pRemainingUseDuration);
    }

    @Override
    public boolean hurtEnemy(ItemStack pStack, LivingEntity pTarget, LivingEntity pAttacker) {
        pTarget.setSecondsOnFire(3);
        return super.hurtEnemy(pStack, pTarget, pAttacker);
    }

    @Override
    public boolean onLeftClickEntity(ItemStack stack, Player player, Entity entity) {
        playAttackSounds(player);
        return super.onLeftClickEntity(stack, player, entity);
    }


    @Override
    public boolean mineBlock(ItemStack pStack, Level pLevel, BlockState pState, BlockPos pPos, LivingEntity pEntityLiving) {
        playAttackSounds(pEntityLiving);
        return super.mineBlock(pStack, pLevel, pState, pPos, pEntityLiving);
    }

    @Override
    public int getUseDuration(ItemStack pStack) {
        return 40;
    }

    @Nonnull
    @Override
    public UseAnim getUseAnimation(ItemStack pStack) {
        return UseAnim.BOW;
    }


}
